/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;
import com.neusoft.lohas.modules.ivm.entity.product.Supplier;
import com.neusoft.lohas.modules.ivm.service.product.SupplierService;
import com.neusoft.lohas.modules.ivm.utils.MerchantUtils;

/**
 * 供应商Controller
 * @author Jackie.Liu
 * @version 2014-12-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/supplier")
public class SupplierController extends BaseController {

	@Autowired
	private SupplierService supplierService;
	
	@ModelAttribute
	public Supplier get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return supplierService.get(id);
		}else{
			return new Supplier();
		}
	}
	
	@RequiresPermissions("ivm:supplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(Supplier supplier, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			supplier.setCreateBy(user);
		}
        Page<Supplier> page = supplierService.find(new Page<Supplier>(request, response), supplier); 
        model.addAttribute("page", page);
		return "modules/ivm/pro/supplierList";
	}

	@RequiresPermissions("ivm:supplier:view")
	@RequestMapping(value = "form")
	public String form(Supplier supplier, Model model) {
		model.addAttribute("supplier", supplier);
		return "modules/ivm/pro/supplierForm";
	}

	@RequiresPermissions("ivm:supplier:edit")
	@RequestMapping(value = "save")
	public String save(Supplier supplier, String oldName, 
			Model model, RedirectAttributes redirectAttributes) {
		supplier.setMerchant(super.merchant);
		if (!beanValidator(model, supplier)){
			return form(supplier, model);
		}
		if (!"true".equals(checkName(oldName, supplier.getName()))){
			addMessage(model, "保存供应商'" + supplier.getName() + "'失败，供应商已存在");
			return form(supplier, model);
		}
		supplierService.save(supplier);
		addMessage(redirectAttributes, "保存供应商'" + supplier.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/supplier/?repage";
	}
	
	@RequiresPermissions("ivm:supplier:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		supplierService.delete(id);
		addMessage(redirectAttributes, "删除供应商成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/supplier/?repage";
	}
	
	@ResponseBody
	@RequiresPermissions("ivm:supplier:edit")
	@RequestMapping(value = "checkName")
	public String checkName(String oldName, String name) {
		if (name !=null && name.equals(oldName)) {
			return "true";
		} else if (name !=null && supplierService.getSupplierByName(MerchantUtils.getMerchant().getId(), name) == null) {
			return "true";
		}
		return "false";
	}

}
